package com.jrx.anytxn.param.service.investor.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.constant.CommonContants;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.investor.PrInvestorPenaltyRateReq;
import com.jrx.anytxn.param.dto.investor.PrInvestorPenaltyRateRes;
import com.jrx.anytxn.param.entity.PrInvestorPenaltyRate;
import com.jrx.anytxn.param.mapper.ext.ExtPrInvestorPenaltyRateMapper;
import com.jrx.anytxn.param.service.investor.IPrInvestorPenaltyRateService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 出资方罚息利率表 业务接口实现
 *
 * @author 黄秋平
 * @date 2020年5月18日17:22:06
 */
@CacheConfig(cacheNames = "INVESTOR_PENALTY")
@Service
public class PrInvestorPenaltyRateServiceImpl implements IPrInvestorPenaltyRateService {

    private static final Logger logger = LoggerFactory.getLogger(PrInvestorPenaltyRateServiceImpl.class);

    @Resource
    private ExtPrInvestorPenaltyRateMapper extPrInvestorPenaltyRateMapper;

    @Resource
    private ISegmentService segmentService;


    @Override
    public TxnPage<PrInvestorPenaltyRateRes> findAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<PrInvestorPenaltyRate> page = PageHelper.startPage(pageNum, pageSize,Constant.ORDER_BY_SORT);
        List<PrInvestorPenaltyRate> penaltyRateTableList = extPrInvestorPenaltyRateMapper.selectByTenId(tenantId);
        if (penaltyRateTableList.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PrInvestorPenaltyRateRes> res = BeanMapping.copyList(penaltyRateTableList, PrInvestorPenaltyRateRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public PrInvestorPenaltyRateRes addPenaltyRateTable(PrInvestorPenaltyRateReq prInvestorPenaltyRateReq) throws TxnException {
        PrInvestorPenaltyRate PrInvestorPenaltyRate = extPrInvestorPenaltyRateMapper.selectByTenIdAndPanaltyId(prInvestorPenaltyRateReq.getTenantId(), prInvestorPenaltyRateReq.getInvestorPenaltyId());
        if (PrInvestorPenaltyRate != null) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        PrInvestorPenaltyRate penaltyRateTable = BeanMapping.copy(prInvestorPenaltyRateReq, PrInvestorPenaltyRate.class);
        try {
            penaltyRateTable.setId(segmentService.getId(Constant.PR_INVESTOR_PENALTY_RATE));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        penaltyRateTable.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrInvestorPenaltyRateMapper.insertSelective(penaltyRateTable);
        return BeanMapping.copy(penaltyRateTable, PrInvestorPenaltyRateRes.class);
    }

    @CacheEvict(key = "'investorPenalty_'+#prInvestorPenaltyRateReq.tenantId+'_'+#prInvestorPenaltyRateReq.investorPenaltyId")
    @Override
    public PrInvestorPenaltyRateRes modifyPenaltyRateTable(PrInvestorPenaltyRateReq prInvestorPenaltyRateReq) throws TxnException {
        PrInvestorPenaltyRate PrInvestorPenaltyRate = extPrInvestorPenaltyRateMapper.selectByTenIdAndPanaltyId(prInvestorPenaltyRateReq.getTenantId(), prInvestorPenaltyRateReq.getInvestorPenaltyId());
        if (PrInvestorPenaltyRate == null) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }

        PrInvestorPenaltyRate penaltyRateTable = BeanMapping.copy(prInvestorPenaltyRateReq, PrInvestorPenaltyRate.class);
        // 更新
        extPrInvestorPenaltyRateMapper.updateByTenIdAndPenaltyId(penaltyRateTable);
        return BeanMapping.copy(penaltyRateTable, PrInvestorPenaltyRateRes.class);

    }

    @CacheEvict(key = "'investorPenalty_'+#tenantId+'_'+#investorPenaltyId")
    @Override
    public Boolean removeByTenIdAndPenaltyId(String tenantId, String investorPenaltyId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(investorPenaltyId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPrInvestorPenaltyRateMapper.deleteByTenIdAndPenaltyId(tenantId, investorPenaltyId);
        return i > 0;
    }

    @Override
    public PrInvestorPenaltyRateRes findByTenIdAndPenaltyId(String tenantId, String investorPenaltyId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(investorPenaltyId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrInvestorPenaltyRate penaltyRateTable = extPrInvestorPenaltyRateMapper.selectByTenIdAndPanaltyId(tenantId, investorPenaltyId);
        return BeanMapping.copy(penaltyRateTable, PrInvestorPenaltyRateRes.class);
    }

    @Cacheable(key = "'investorPenalty_'+#tenantId+'_'+#investorPenaltyId")
    @Override
    public PrInvestorPenaltyRate findPrInvestorPenaltyRate(String tenantId, String investorPenaltyId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(investorPenaltyId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrInvestorPenaltyRate penaltyRateTable = extPrInvestorPenaltyRateMapper.selectByTenIdAndPanaltyId(tenantId, investorPenaltyId);
        return penaltyRateTable;
    }

    /**
     * 获取罚息日利率
     *
     * @param dailyRate           日利率
     * @param investorPenaltyRate 罚息参数
     * @return BigDecimal 罚息日利率
     * @author liuyang
     * @date 2020-05-27
     */
    @Override
    public BigDecimal getPenaltyDailyRate(BigDecimal dailyRate, PrInvestorPenaltyRate investorPenaltyRate) {
        BigDecimal penDailyRate = BigDecimal.ZERO;

        if (investorPenaltyRate == null) {
            return penDailyRate;
        }
        String increaseType = investorPenaltyRate.getIncreaseType();
        BigDecimal increaseNum = investorPenaltyRate.getIncreaseNum();

        //上浮百分比
        if (Constant.PENALTY_RATE_INCREASE_TYPE_PERCENT.equals(increaseType)) {
            BigDecimal percent = increaseNum.add(BigDecimal.ONE);
            penDailyRate = dailyRate.multiply(percent).setScale(CommonContants.BIGDECIMAL_SCALE_7, BigDecimal.ROUND_HALF_UP);
        } else {//上浮固定值
            penDailyRate = dailyRate.add(increaseNum).setScale(CommonContants.BIGDECIMAL_SCALE_7, BigDecimal.ROUND_HALF_UP);
        }
        return penDailyRate;
    }
}
